﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.supply
{
    public partial class AddMonthSupplyInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            if (Session["supplyPlanIdAddMonth"] != null)
            {
                string supplyPlanId = Session["supplyPlanIdAddMonth"].ToString();
                DataSet ds = new Maticsoft.BLL.supplyPlan().GetList(" supplyPlanId="+supplyPlanId);

                this.lbl_material.Text = ds.Tables[0].Rows[0]["material"].ToString();
                this.lbl_nums.Text = ds.Tables[0].Rows[0]["num"].ToString();
                this.lbl_proid.Text = ds.Tables[0].Rows[0]["proid"].ToString();
                this.lbl_projectName.Text = ds.Tables[0].Rows[0]["projectName"].ToString();
                this.txt_supplyDate.Text = ds.Tables[0].Rows[0]["supplyDate"].ToString();


                DataSet ds_innums = new Maticsoft.BLL.supplyPlan().GetListAllInums(" supplyPlanId=" + supplyPlanId);

                //计划总入库量
                if (ds_innums.Tables[0].Rows.Count == 1)
                {
                    this.lbl_innums.Text = ds_innums.Tables[0].Rows[0]["innums"].ToString();
                }
                else
                {
                    this.lbl_innums.Text = "0";
                }

                this.txt_thisMonthCount.Text = lbl_nums.Text;

                List<lcs.Model.configinfo> cm = new lcs.BLL.configinfo().GetModelList("");

                if (cm.Count == 1)
                {
                    lcs.Model.configinfo c = cm[0];

                    this.lbl_se.Text = "*注：当月供应计划起始日期：本月" + c.ygyqs + "号，截止日期：下月" + c.ygyjs + "号";
                }

                //DataSet ds_storeInfo = new lcs.BLL.storeinfo().GetList("");

                //this.ddl_arrivalAddress.DataTextField = "sname";
                //this.ddl_arrivalAddress.DataValueField = "sname";

                //this.ddl_arrivalAddress.DataSource = ds_storeInfo;
                //this.ddl_arrivalAddress.DataBind();

                //this.ddl_arrivalAddress.Items.Insert(0,new ListItem("---未选择----","-1"));

                DataSet ds_equipmentManage = new lcs.BLL.equipmentManage().GetList("");

                this.ddl_equipmentManage.DataTextField = "equipmentManageName";
                this.ddl_equipmentManage.DataValueField = "equipmentManageName";

                this.ddl_equipmentManage.DataSource = ds_equipmentManage;
                this.ddl_equipmentManage.DataBind();

                this.ddl_equipmentManage.Items.Insert(0, new ListItem("---未选择----", "-1"));

                this.ddl_equipmentManage.SelectedValue = ds.Tables[0].Rows[0]["equipmentManage"].ToString();


            }
        }

        //生成本月供应计划
        protected void btn_addmo_Click(object sender, EventArgs e)
        {
            if (Session["supplyPlanIdAddMonth"] != null)
            {
                if (string.IsNullOrEmpty(this.txt_supplyDate.Text))
                {
                    Maticsoft.Common.MessageBox.Show(this, "请填写供应年月！");
                    return;
                }
                if (string.IsNullOrEmpty(this.txt_thisMonthCount.Text))
                {
                    Maticsoft.Common.MessageBox.Show(this, "请填写本月计划数量！");
                    return;
                }
                //if (this.ddl_arrivalAddress.SelectedValue == "-1")
                //{
                //    Maticsoft.Common.MessageBox.Show(this, "请选择到货仓库！");
                //    return;
                //}
                if (this.ddl_equipmentManage.SelectedValue == "-1")
                {
                    Maticsoft.Common.MessageBox.Show(this, "请选择设备管理单位！");
                    return;
                }
                string supplyPlanId = Session["supplyPlanIdAddMonth"].ToString();
                Maticsoft.Model.supplyPlan s = new Maticsoft.BLL.supplyPlan().GetModel(int.Parse(supplyPlanId));
                decimal count = 0;
                decimal innums = 0;
                decimal maxNums = 0;
                try
                {
                    count = decimal.Parse(this.txt_thisMonthCount.Text);
                    innums = decimal.Parse(this.lbl_innums.Text);//计划已入库数量

                    maxNums = s.num.Value - innums;

                    if (count <= maxNums)//小于等于总计划数量
                    {
                        Maticsoft.BLL.monthSupplayPlan msm = new Maticsoft.BLL.monthSupplayPlan();
                        Maticsoft.Model.monthSupplayPlan ms = new Maticsoft.Model.monthSupplayPlan();

                        //ms.arrivalAddress = this.ddl_arrivalAddress.SelectedValue;
                        ms.equipmentManage = this.ddl_equipmentManage.SelectedValue;//设备管理单位
                        ms.contactWay = this.txt_contactWay.Text;
                        ms.linkman = this.txt_linkman.Text;
                        ms.supplyDate = DateTime.Parse(this.txt_supplyDate.Text);//供应年月
                        ms.thisMonthCount = count;
                        ms.supplyPlanId = int.Parse(supplyPlanId);
                        ms.status = "1";
                        if (this.RadioButton1.Checked)
                        {
                            ms.isGoodsQualityStatus = true;
                        }
                        else
                        {
                            ms.isGoodsQualityStatus = false;
                        }
                        ms.result = "未完成";
                        ms.createTime = DateTime.Now;//计划生成时间

                        if (msm.Add(ms) > 0)
                        {
                            //Maticsoft.Common.MessageBox.Show(this, "增加本月供应计划成功！");
                            //Maticsoft.Common.MessageBox.ShowAndRedirect(this, "增加本月供应计划成功！", "ListMonthSupply.aspx");

                            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.returnValue=true;parent.close();</script>");
                        }
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.Show(this, "本月计划数量应小于等于(总计划数量-已入库数量)！");
                    }                    
                }
                catch (Exception)
                {
                    Maticsoft.Common.MessageBox.Show(this, "计划数量请填写数字！");
                }
            }           
        }
    }
}